<?php
// +----------------------------------------------------------------------
// | makePHP 
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://makephp.org All rights reserved.
// +----------------------------------------------------------------------
// | Licensed BSD.(http://www.make.org/BSD-license.txt)
// +----------------------------------------------------------------------
// | Author: 叁石(sanshi) <sanshi0815@tom.com>
// | Author: 白家昌<baijc2004cn@126.com>
// +----------------------------------------------------------------------
/**
 *  Entrance File of the Control class.
 *
 * @Author:    <>
 * @Package:   Interface of the View class. 
 * @Version:   $Id: zy_materials_from_list.class.php 2011-07-11 11:23:32 $
 * @Show Note: 
 */
Class zy_materials_from_list extends TheAdmin
{
	/* DataBase object */
	public $db;

	/* View the data page limit items */
	public $pageSize = PAGENO;
	
	/**
	 *	Construct this module class.
	*/
	public function zy_materials_from_list()
	{
	}
	public function set()
	{
		$this->TheAdmin();
		//定义模版
		$this->tplFile ="zy_materials_from_list.tpl";
		$this->admin_user_id = $this->userRequest->get('admin_user_id');
		//取得数据库连接
		$this->db = Factory::getDB("zy_materials_from_list");
		//取得用户对于该类的权限
		$user_p = $this->get_user_popedom_by_class_name(__CLASS__);
		$class_p_arr = $this->show_p();
		//取得该类的权值数组
		$class_method = $class_p_arr['class_method'];
		//取得用户操作菜单
		$menu = array();
		foreach ($class_method as $k=>$v)
		{
			if( (parse_power($user_p,$v[2]) && (isset($v[3]) ?$v[3] :false)) || IS_ROOT)
			{
				array_push($menu,array('url'=>"?c=".__CLASS__."&m={$v[0]}",'title'=>$v[1]));
			}
		}
		//用户有权菜单生成 可以添加其他操作
		$this->smarty->assign('menus',$menu);
		$this->smarty->assign('pay_type_arr',get_pay_type_arr());//付款方式
		$this->smarty->assign('transport_type_arr',get_transport_type());//运输方式
		$this->smarty->assign("main_menus",$this->get_user_menus());
	}
	
	/**
	 *	Call class construct method.
	*/
	public function __construct()
	{
			$this->zy_materials_from_list();
	}
	public function _display()
	{
		$this->smarty->display($this->tplFile);
	}
	/**
	 *	default method.
	*/
	public function show()
	{
		$col = "list_id";
		$where = 1;
		$rowNo = $this->db->getSumNumber($this->db->tableName,$where);
		//数据库执行sql语句出错
		//取得错误的sql语句
		//$error_sql = end($this->db->getDebugMsg());
		//echo $error_sql;
		if( 0 > $rowNo) goToIndex($rowNo);
		//分页
		$page = Factory::getBaseClass('pagination');
		$page->set($rowNo,$this->pageSize);
		$numrows = $this->pageSize;
		$offset  = $page->getSqlMove();
		$arr = $this->db->getAll($where,$col,$numrows,$offset);
		admin_log($this->admin_user_id,__METHOD__,'查看',1);
		//数据库执行select语句出错
		//取得错误的sql语句
		//$error_sql = end($this->db->getDebugMsg());
		//echo $error_sql;
		if(0 > $arr ) goToIndex($arr);
		
		$this->smarty->assign('arr',$arr);
		//默认方法
		$this->smarty->assign("page_footer",$page->getFooter().$page->getJumpPage());
		$this->smarty->assign('act','list');
		$this->smarty->display($this->tplFile);
	}
	
	/**
	 *	add method.
	*/
	public function add()
	{
		$materials_from_id = isset($_REQUEST['materials_from_id']) ? intval($_REQUEST['materials_from_id']) : 0;//自购物资ID
		//添加数据
		if(!isset($_REQUEST['post']) || trim($_REQUEST['post'])!=1)
		{
			if (empty($materials_from_id)) {
				alertMsg('-5003');
			}
			//使用模版，展示提交界面
			$this->smarty->assign('materials_from_id',$materials_from_id);
			$this->smarty->assign('act_title','添加');
			$this->smarty->assign('act','add');
			$this->smarty->display($this->tplFile);
		} else {
			//用户提交，把数据插入数据库
			$temp = $this->getValue();
			$insrt = $this->db->insert_zy_materials_from_list($temp);
			$error_no = 0 > $insrt ?  $insrt :'-1501';
			admin_log($this->admin_user_id,__METHOD__,"id={$insrt}",2);
			//取得错误的sql语句
			//$error_sql = end($this->db->getDebugMsg());
			//echo $error_sql;
			if ($insrt < 0) {
				alertMsg($error_no,"index.php?c=zy_materials_from&m=browse&materials_from_id={$materials_from_id}");
			}else{
				redirectPage("index.php?c=zy_materials_from&m=browse&materials_from_id={$materials_from_id}");
			}
			//alertMsg($error_no, 'index.php?c=zy_materials_from');
		}
	}
	
	/**
	 *	edit method.
	*/
	public function edit()
	{
		//取得id，也就是唯一索引
		$list_id = isset($_REQUEST['list_id']) ? intval(trim($_REQUEST['list_id'])) : 0 ;
		//取得是否查看详情
		$is_show = isset($_REQUEST['is_show']) ? intval(trim($_REQUEST['is_show'])) : 0 ;
		$materials_from_id = isset($_REQUEST['materials_from_id']) ? intval($_REQUEST['materials_from_id']) : 0;//自购物资ID
		if(!isset($_REQUEST['post']) || trim($_REQUEST['post'])!=1)
		{
			//使用模版，展示提交界面
			$result = $this->db->getOne($list_id);
			if(empty($result))
			{
				alertMsg('-2001', 'index.php?c=zy_materials_from_list');
			}
			foreach ($result AS $k=>$v)
			{
				$this->smarty->assign("{$k}",$v);
			}
			$this->smarty->assign('is_show',$is_show);
			$this->smarty->assign('act_title','修改');
			$this->smarty->assign('act','edit');
			$this->smarty->display($this->tplFile);
		} else {
			//用户提交，把数据插入数据库
			//设置where条件
			$where = "list_id=$list_id";
			$temp = $this->getValue(1);
			$update = $this->db->update_zy_materials_from_list($temp,$where);
			admin_log($this->admin_user_id,__METHOD__,$where,2);
			$error_no = 0 > $update ?  $update :'-1502';
			//取得错误的sql语句
			//$error_sql = end($this->db->getDebugMsg());
			//echo $error_sql;
			if ($update < 0) {
				alertMsg($error_no, "index.php?c=zy_materials_from&m=browse&materials_from_id={$materials_from_id}");
			}else{
				redirectPage("index.php?c=zy_materials_from&m=browse&materials_from_id={$materials_from_id}");
			}
			//alertMsg($error_no, 'index.php?c=zy_materials_from');
		}
	}
	
	/**
	 *	browse method.
	*/
	public function browse() {
		$list_id = isset($_REQUEST['list_id']) ? intval(trim($_REQUEST['list_id'])) : 0 ;
		//使用模版，展示提交界面
		$result = $this->db->getOne($list_id);
		if(empty($result))
		{
			alertMsg('-2001', 'index.php?c=zy_materials_from');
		}
		foreach ($result AS $k=>$v)
		{
			$this->smarty->assign("{$k}",$v);
		}
		//$this->smarty->assign('is_show',$is_show);
		$this->smarty->assign('act_title','查看');
		$this->smarty->assign('act','browse');
		$this->smarty->display($this->tplFile);
	}
	
	/**
	 *	del method.
	*/
	public function del()
	{
		//取得id，也就是唯一索引
		$list_id = isset($_REQUEST['list_id']) ? intval(trim($_REQUEST['list_id'])) : 0 ;
		$materials_from_id = isset($_REQUEST['materials_from_id']) ? intval($_REQUEST['materials_from_id']) : 0;//自购物资ID
		$where = "list_id=$list_id";
		$del=$this->db->delete_zy_materials_from_list($where);
		admin_log($this->admin_user_id,__METHOD__,$where,3);
		$error_no = 0 > $del ?  $del :'-1503';
		//取得错误的sql语句
		//$error_sql = end($this->db->getDebugMsg());
		//echo $error_sql;
		if ($del < 0) {
			alertMsg($error_no, "index.php?c=zy_materials_from&m=browse&materials_from_id={$materials_from_id}");
		}else{
			redirectPage("index.php?c=zy_materials_from&m=browse&materials_from_id={$materials_from_id}");
		}
		//alertMsg($error_no, 'index.php?c=zy_materials_from');
	}
	
	/**
	 *	get request method.
	*/
	public function &getValue($isEdit=0)
	{
		$temp = array();
		$check = Factory::getBaseClass("checkBase");
		if($isEdit){
			$temp['list_id'] = $_POST['list_id'];
		} else {
			$temp['list_id'] = "";
			$temp['materials_from_id'] = isset($_POST['materials_from_id']) ? intval($_POST['materials_from_id']) : 0;//自购物资ID
			if (empty($temp['materials_from_id'])) {
				alertMsg('-5003');
			}
		}
		//补充其他的字段
		$temp['materials_name'] = isset($_POST['materials_name']) ? htmlspecialchars(trim($_POST['materials_name'])) : '';//物资名称
		if (empty($temp['materials_name'])) {
			alertMsg('-5004');
		}
		$temp['specification'] = isset($_POST['specification']) ? htmlspecialchars(trim($_POST['specification'])) : '';//规格型号
		$temp['number'] = isset($_POST['number']) ? intval($_POST['number']) : 0;//数量
		$temp['price'] = isset($_POST['price']) ? sprintf("%01.2f",$_POST['price']) : 0;//单价
		$temp['pay_type'] = isset($_POST['pay_type']) ? intval($_POST['pay_type']) : 1;//付款方式
		$temp['contract_no'] = isset($_POST['contract_no']) ? htmlspecialchars(trim($_POST['contract_no'])) : '';//合同号
		/*if (empty($temp['contract_no'])) {
			alertMsg('-5005');
		}*/
		$temp['m_unit'] = isset($_POST['m_unit']) ? htmlspecialchars(trim($_POST['m_unit'])) : '';//单位
		$temp['info'] = isset($_POST['info']) ? htmlspecialchars(trim($_POST['info'])) : '';//备注
		$temp['type_id'] = isset($_POST['type_id']) ? intval($_POST['type_id']) : 1;//运输ID
		$temp['project_id'] = isset($_POST['project_id']) ? intval($_POST['project_id']) : 1;//项目ID
				
		admin_log($this->admin_user_id,__METHOD__,serialize($temp),2);	
		return $temp;
	}
	/**
	 *	get class method and popedom.
	*/
	public function show_p()
	{
		//该方法为，提供用户使用方法定义，非web用户权限相关不用写
		return array(
			/* 定义该类中文名称*/
			'class_name'=>array(__CLASS__,'自购物资列表管理'),
			'class_method' =>array(
				//array（方法名，中文解释，权值,是否为菜单）
				'show'  => array('show','列表',1,true),
				'add'  => array('add','添加',2,true),
				'edit'  => array('edit','编辑',4,false),
				'del'  => array('del','删除',8,false),
				'browse' => array('browse', '查看', 16, false),
				'show_p'=> array('show_p','',0,false)
			)
		);
	}
}

?>